using Avalonia.BiliBili.ViewModels;
using Avalonia.Extensions.Controls;
using Avalonia.Extensions.Event;
using Avalonia.Input;
using Avalonia.Interactivity;
using Bilibili.Common;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.Model.Live;
using Bilibili.WebClient.Api;
using DynamicData;
using PCLUntils.Objects;
using System;
using System.Diagnostics;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.Windows
{
    public partial class LiveFocusWindow : WindowBase
    {
        private LiveCenterClient Client { get; }
        private int PageIndex { get; set; } = 1;
        private int CurrentPage { get; set; } = 1;
        private LiveFocusViewModel ViewModel { get; }
        public LiveFocusWindow()
        {
            InitializeComponent();
            Client = new LiveCenterClient();
            ViewModel = new LiveFocusViewModel();
        }
        protected override void AfterInitializeComponent()
        {
            this.DataContext = ViewModel;
            livingList.ItemClick += LivingList_ItemClick;
            notLiving.ScrollEnd += NotLiving_ScrollEnd;
            liveHistory.ScrollEnd += LiveHistory_ScrollEnd;
            liveHistory.ItemClick += LiveHistory_ItemClick;
            RunOnUiThread(async () =>
            {
                await LiveData();
                await NotLiveData();
                await GetHistoryList();
            });
        }
        private void LiveHistory_ItemClick(object? sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickMouse == MouseButton.Left && e.ClickItem is GridViewItem item && item.Content is LiveHistoryItemModel model)
                    App.Mapper.NavigateToPlayer(model.Roomid, PlayMode.Live);
            }
            catch { }
        }
        private void LivingList_ItemClick(object? sender, ViewRoutedEventArgs e)
        {
            try
            {
                if (e.ClickMouse == MouseButton.Left && e.ClickItem is GridViewItem item && item.Content is LivingModel model)
                    App.Mapper.NavigateToPlayer(model.roomid, PlayMode.Live);
            }
            catch { }
        }
        private void LiveHistory_ScrollEnd(object? sender, RoutedEventArgs e)
        {
            PageIndex++;
            RunOnUiThread(GetHistoryList);
        }
        private void NotLiving_ScrollEnd(object? sender, RoutedEventArgs e)
        {
            CurrentPage++;
            RunOnUiThread(NotLiveData);
        }
        private async Task NotLiveData()
        {
            try
            {
                var model = await Client.GetUnLiveList(CurrentPage);
                if (model.IsSuccess())
                {
                    var array = model.Data;
                    if (array != null && array.Count > 0)
                        ViewModel.NotLivings.AddRange(array);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("NotLiveData", ex);
            }
        }
        private async Task LiveData()
        {
            try
            {
                var model = await Client.GetLiveList();
                if (model.IsSuccess())
                {
                    var array = model.Data;
                    if (array != null && array.Count > 0)
                        ViewModel.Livings.AddRange(array);
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("LiveData", ex);
            }
        }
        private async Task GetHistoryList()
        {
            try
            {
                var model = await Client.GetHistory(PageIndex);
                if (model.IsNotEmpty())
                {
                    if (model.IsSuccess())
                    {
                        var array = model.Data;
                        if (array != null && array.Count > 0)
                            ViewModel.LiveHistories.AddRange(array);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("GetHistoryList", ex);
            }
        }
    }
}